﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Windows.Threading;
using Sebarf.Diagnostics.Interfaces;
using Sebarf.Services.Interfaces;
using Sebarf.Session.Interfaces;

#endregion

namespace Sebarf.Session.Services {
	/// <summary>
	/// Description of the class
	/// </summary>
	public class SessionTimeoutHandlerService : Service, ISessionTimeoutHandlerService {
		#region Public Properties

		[ServiceRequest]
		public IUserSessionHandlerService UserSessionHandlerService { get; set; }

		public TimeSpan SessionCloseTimeout { get; set; }

		#endregion

		#region Public Methods

		public SessionTimeoutHandlerService() {
			SessionCloseTimeout = TimeSpan.FromMinutes(30);
		}

		public override void DoSetup() {
			base.DoSetup();
			m_timer = new DispatcherTimer();
			m_timer.Interval = SessionCloseTimeout;
			m_timer.Tick += OnTimerTick;
			m_timer.Start();
		}

		public override void DoShutdown() {
			base.DoShutdown();
			m_timer.Tick -= OnTimerTick;
			m_timer.Stop();
		}

		#endregion

		#region Private Methods

		private void OnTimerTick(object sender, EventArgs e) {
			UserSessionHandlerService.RemoveSession();
		}

		#endregion

		#region Private Fields

		private DispatcherTimer m_timer;

		#endregion

		#region ISessionTimeoutHandlerService Members

		public void ResetTimeout() {
			Logger.WriteInformation("Reset session timeout");
			if (m_timer != null) {
				m_timer.Stop();
				m_timer.Start();
			}
		}

		#endregion
	}
}